class Solution:
    def plusOne(self, digits):
        if len(digits) == 1:
            if digits[0] == 9: return [1, 0]
            return [digits[0]+1]

        flag = True
        for i in range(len(digits)-1, 0, -1):
            print(digits[i], digits[i-1])
            if digits[i] != 9:
                digits[i] += 1
                flag = False
                break
            if digits[i] == 9 and digits[i-1] != 9 and i > 0:
                digits[i] = 0
                digits[i-1] += 1
                flag = False
                break
            if digits[i] == 9 and digits[i-1] == 9 and i > 0:
                digits[i] = 0
        if not flag:    return digits
        return [1] + [0] * len(digits)
if __name__ == '__main__':
    s = Solution()
    L = [9, 9, 9]
    L = [9, 8, 9]
    L = [1, 9]
    L = [9]

    L = [0]
    # L = [1, 2, 3]
    res = s.plusOne(L)
    print(res)